Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Realtime decoding #615

Closed
wants to merge 82 commits into from
Closed

WIP: Realtime decoding #615

wants to merge 82 commits into from

Conversation

mainakjas
Copy link
Contributor

This is being built on top of @mluessi's PR: #382. First attempt at building something is a simple decoding example using SVM. My plan is to move much of the code into functions such as rtSVM. It seems to work, but I am surprised at uniformly 100% accuracies - either something wrong with the code or we need to change the categories being decoded.

while 1:

# Fetch epochs and labels
if (ii==0):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really ugly. I wish I could do without an if condition. Something like epochs = [] outside the loop and then np.append() inside the loop, but that doesn't work ...

@agramfort
Copy link
Member

do we have some python code to mock the real time server? it will be required for testing.

@mluessi
Copy link
Contributor

mluessi commented Jun 5, 2013

Not yet.. I will look into implementing it, should be quite straightforward.

@mluessi
Copy link
Contributor

mluessi commented Jun 6, 2013

@mainakjas some general comments. I think it would be good if in the first phase of your gsoc project we go "back to the basics" and first do some experiments with offline learning, this will allow you to see how well different classifiers work, test feature extraction methods, etc. Also, the real-time part currently needs the mne_rt_server, which makes it more difficult for others to run your examples. I will implement a mock server, so we will be able to have standalone python examples.

Regarding the code, make sure you use a pep8 checker, so your code formatting is coherent with the rest of the code (mostly there are many spaces missing).

@mainakjas
Copy link
Contributor Author

thanks for noticing, the PEP8 checker seems to be disabled/broken. I'll check that :)

@chdinh
Copy link
Member

chdinh commented Jun 6, 2013

@mluessi if you don't want to implement the mock server. You can static build mne_rt_server and provide it as standalone application to the others.

@chdinh
Copy link
Member

chdinh commented Jun 6, 2013

@agramfort i think its not required - since we are very cross-platform even for embedded systems. You can just static build the server - this would be similiar to the fieldtrip buffer. They provide the bins with the matlab code.

@mluessi
Copy link
Contributor

mluessi commented Jun 6, 2013

@chdinh I don't think that's an option.. it will make unit testing of mne-python difficult. It shouldn't be very hard to implement a mock server in Python that just implements the file simulation.

@chdinh
Copy link
Member

chdinh commented Jun 6, 2013

okay, didn't thought of the unit testing. - Just for my understanding: is it possible to call executables with python code?

@mainakjas
Copy link
Contributor Author

I had a hard time setting up things for the forward model unit tests.
Doesn't that rely on calling executables from python?

On Thu, Jun 6, 2013 at 7:51 PM, Christoph Dinh notifications@github.comwrote:

okay, didn't thought of the unit testing. - Just for my understanding: is
it possible to call executable with python code?


Reply to this email directly or view it on GitHubhttps://github.com//pull/615#issuecomment-19058559
.

@larsoner
Copy link
Member

larsoner commented Jun 6, 2013

@chdinh it is possible, see the run_subprocess function in utils.py. For an example see the code for mne.do_forward_solution. We try to avoid using this function as much as possible, though, since it depends on people not just having software installed on their system, but also having paths and such set correctly when python is executed (see @mainakjas's issues, for example).

@chdinh
Copy link
Member

chdinh commented Jun 6, 2013

thx - same in mne-cpp everything should be written in Qt to not depend on other libs to avoid such issues. Anyway another issue I see is: having a real-time simulator for sampling frequencies up to 5k and doesn't have the ability to parallelize in multiple threads will reduce the possbile calculations per second which mainakjas could use for the classification process. If the simulation is done by mne_rt_server (which allone uses 5 threads), the CPU Core power could be totally used for the classification purpose.

@agramfort
Copy link
Member

+100 for a mock server.

1/ if we don't the code will be not be tested on a regular basis
2/ the code will not be used on non production systems eg. for prototyping
and small experiments
3/ you don't want people harassing you to get binaries for their old ubuntu
/ mandriva system on which they don't have admin rights. Believe me I have
the problem on a regular basis for OpenMEEG...

Real-time M/EEG Acquisition
---------------------------

Receive data from an MNE Real-time sever (mne_rt_server, part of MNE-CPP),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sever -> server

@agramfort
Copy link
Member

I think we really need to sit down and discuss API and use cases.

In an online setting you would update the classifier rather than recompute everything
and report the score ie classif accuracy for each new batch of 5 epochs or something.

is there a way to command the stimulation device ie. do some kind of reinforcement learning?
ask for epochs with event_id badly predicted?

@mluessi
Copy link
Contributor

mluessi commented Jun 6, 2013

Yes, I agree. I think once we know the use cases, it will become more clear what API should be used. I think it would also be good to first do some experiments without the real-time part. E.g., how many epochs do we need to accurately predict the event types, what features work best, etc.

Does scikit-learn support online learning, i.e., can classifiers be updated with new samples? I don't think this is the case but it would be very nice..

Regarding reinforcement learning, do you mean modifying the experiment based on the classifier output ("feedback")? This would definitely be nice. I think we should find a way to interface with PsychoPy. The main difficulty is probably that it will run as a separate process, so we could use TCP/IP for interprocess communication.

@mainakjas
Copy link
Contributor Author

+1 to discussing the use cases.

same question as martin, can classifiers be updated with new samples in
scikit-learn? I thought it wasn't so easy with some classifiers like SVM ...

ps: Another use case (this just an idea) I was thinking is the ability to
do try multiple classifiers/parameters at the same time (or in parallel).
So, the methods should ideally be able to provide such functionality that
it can give accuracies with SVM, NaiveBayes and LDA at the same time; or
SVM with different soft-margin parameters. So, some kind of tuning the
parameters online so that the experimenter knows immediately if the
experiment is going to work or not, with all these different possibilities.

On Thu, Jun 6, 2013 at 11:31 PM, Martin Luessi notifications@github.comwrote:

Yes, I agree. I think once we know the use cases, it will become more
clear what API should be used. I think it would also be good to first do
some experiments without the real-time part. E.g., how many epochs do we
need to accurately predict the event types, what features work best, etc.

Does scikit-learn support online learning, i.e., can classifiers be
updated with new samples? I don't think this is the case but it would be
very nice..

Regarding reinforcement learning, do you mean modifying the experiment
based on the classifier output ("feedback")? This would definitely be nice.
I think we should find a way to interface with PsychoPy. The main
difficulty is probably that it will run as a separate process, so we could
use TCP/IP for interprocess communication.


Reply to this email directly or view it on GitHubhttps://github.com//pull/615#issuecomment-19072055
.

@agramfort
Copy link
Member

Yes, I agree. I think once we know the use cases, it will become more clear what API should be used. I think it would also be good to first do some experiments without the real-time part. E.g., how many epochs do we need to accurately predict the event types, what features work best, etc.

Does scikit-learn support online learning, i.e., can classifiers be updated with new samples? I don't think this is the case but it would be very nice..

Grep partial_fit and warm_start in sklearn

Regarding reinforcement learning, do you mean modifying the experiment based on the classifier output ("feedback")? This would definitely be nice. I think we should find a way to interface with PsychoPy. The main difficulty is probably that it will run as a separate process, so we could use TCP/IP for interprocess communication.

Yes.


Reply to this email directly or view it on GitHub.

@agramfort
Copy link
Member

Online feature extraction with morlet or filtering and standard BCI techniques like CSP is of higher priority to me

Simplify realtime API code
event_id, tmin, tmax = 1, -0.2, 0.5

# the number of epochs to average
n_epochs = 30
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, this is no longer required. should delete this too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go ahead !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok fixed this.

what all is left to have this branch merged? We have unit tests and
examples - what else? can we go for a good hard review and aim to have this
merged soon?

On Wed, Jul 17, 2013 at 1:51 PM, Alexandre Gramfort <
notifications@github.com> wrote:

In examples/realtime/plot_compute_rt_moving_average.py:

+import pylab as pl
+
+# Fiff file to simulate the realtime client
+data_path = sample.data_path()
+raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
+raw = mne.fiff.Raw(raw_fname, preload=True)
+
+# select gradiometers
+picks = mne.fiff.pick_types(raw.info, meg='grad', eeg=False, eog=True,

  •                        stim=True, exclude=raw.info['bads'])
    
    +# select the left-auditory condition
    +event_id, tmin, tmax = 1, -0.2, 0.5
    +
    +# the number of epochs to average
    +n_epochs = 30

go ahead !


Reply to this email directly or view it on GitHubhttps://github.com//pull/615/files#r5239140
.

@agramfort
Copy link
Member

Some things I'd like to do before merge:

  • create the mne.decoding module (move classifier.py, mixin.py and the new csp.py in there). Make sure you update the setup.py, the manifest and the python_reference.rst page.
  • then you'll update the whats_new.rst page.
  • also if you don't mind adding denis and me to the Authors on the realtime package as we contributed some code.

any objection on the new mne.decoding module?

@mainakjas
Copy link
Contributor Author

sounds good to me! +1 for mne.decoding module from my end.

On Wed, Jul 17, 2013 at 2:50 PM, Alexandre Gramfort <
notifications@github.com> wrote:

Some things I'd like to do before merge:

  • create the mne.decoding module (move classifier.py, mixin.py and the
    new csp.py in there). Make sure you update the setup.py, the manifest and
    the python_reference.rst page.
  • then you'll update the whats_new.rst page.
  • also if you don't mind adding denis and me to the Authors on the
    realtime package as we contributed some code.

any objection on the new mne.decoding module?


Reply to this email directly or view it on GitHubhttps://github.com//pull/615#issuecomment-21107701
.

@agramfort
Copy link
Member

great ! let me know when I shall review. It should not be long.
Please check the doc page generated with make html to see
if rendering of the docstrings and examples is good.

@mluessi
Copy link
Contributor

mluessi commented Jul 17, 2013

+1 for the decoding module. I think it makes sense to keep realtime and decoding separate.

@agramfort
Copy link
Member

glad we agree. Finishing this PR should be quick then.

@mainakjas go go team !

@dengemann
Copy link
Member

absolutely +1 for all recent proposals.

@mainakjas
Copy link
Contributor Author

ok I was compiling the htmls and it took ages (45 minutes or so) to compile. In the end, when I checked the realtime folder, there was no plot, just the code.

Q1/ Is there an option to compile just one html ? (to save some time)
Q2/ Am I the only one who doesn't see any plot, just the code for the examples in the realtime folder ?

@agramfort
Copy link
Member

ok I was compiling the htmls and it took ages (45 minutes or so) to compile. In the end, when I checked the realtime folder, there was no plot, just the code.

all the examples starting with plot_ should appear.
do you see a "decoding" section in the example page?

I know it takes time to build the doc but it's hard to have real examples that
are all super fast.

Q1/ Is there an option to compile just one html ? (to save some time)

it should only rebuild the examples that failed or which were touched.

Q2/ Am I the only one who doesn't see any plot, just the code for the examples in the realtime folder ?

let me try.

@agramfort
Copy link
Member

I observe the same problem. Plots don't appear in the doc. But read this:


plot_compute_rt_decoder.py is not compiling:
Traceback (most recent call last):
File "/Users/alex/work/src/mne-python/doc/sphinxext/gen_rst.py",
line 650, in generate_file_rst
execfile(os.path.basename(src_file), my_globals)
File "plot_compute_rt_decoder.py", line 113, in
pl.waitforbuttonpress(0.1)
File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/pyplot.py",
line 496, in waitforbuttonpress
return gcf().waitforbuttonpress(_args, *_kwargs)
File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/figure.py",
line 1287, in waitforbuttonpress
return blocking_input(timeout=timeout)
File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/blocking_input.py",
line 402, in call
BlockingInput.call(self,n=1,timeout=timeout)
File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/blocking_input.py",
line 94, in call
self.fig.show()
AttributeError: 'Figure' object has no attribute 'show'

pylab in sphinx does not like the pl.waitforbuttonpress(0.1)
can you replace it with something else?

@mainakjas
Copy link
Contributor Author

Thanks @agramfort, I guess time.sleep() or something like that should be used then.

I created the decoding module now and updated the authors list. Getting closer ...

why do we need to update the manifest.in file? python_reference.rst looks a bit complicated and I need to figure out how things go. More soon!

@agramfort
Copy link
Member

Thanks @agramfort, I guess time.sleep() or something like that should be
used then.

+1

I created the decoding module now and updated the authors list. Getting
closer ...

let me know when done.

why do we need to update the manifest.in file?

indeed no need as you have no data file.

python_reference.rst looks
a bit complicated and I need to figure out how things go. More soon!

ok !

- Fixed one line in the changelog of whats_new.rst
- python_reference.rst was updated but not sure how/which functions
  to include below the Decoding header
@mainakjas
Copy link
Contributor Author

ok, I'm more or less done. Only thing is that I'm confused about python_reference.rst. How should things go there? What functions should be included below the Decoding module and what functions should be included below the Realtime module?

@@ -65,7 +65,8 @@
'mne.tests',
'mne.transforms',
'mne.stats', 'mne.stats.tests',
'mne.time_frequency', 'mne.time_frequency.tests'],
'mne.time_frequency', 'mne.time_frequency.tests',
'mne.realtime', 'mne.decoding'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot to add the tests folders for realtime and decoding

@agramfort
Copy link
Member

besides this LGTM !

@agramfort
Copy link
Member

ok, I'm more or less done. Only thing is that I'm confused about
python_reference.rst. How should things go there? What functions should be
included below the Decoding module and what functions should be included
below the Realtime module?

None as they are all classes as far as I know.

@mainakjas
Copy link
Contributor Author

ok thanks, comments addressed.

On Thu, Jul 18, 2013 at 5:58 PM, Alexandre Gramfort <
notifications@github.com> wrote:

ok, I'm more or less done. Only thing is that I'm confused about
python_reference.rst. How should things go there? What functions should
be
included below the Decoding module and what functions should be included
below the Realtime module?

None as they are all classes as far as I know.


Reply to this email directly or view it on GitHubhttps://github.com//pull/615#issuecomment-21189415
.

@agramfort
Copy link
Member

merged by rebase so we have a clean history !

greaj job @mainakjas !

@agramfort agramfort closed this Jul 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants